<?php
// read topic
//
// digiboard by digitalboom.org
// http://www.digitalboom.org
//
// Information Technology program
// Sirindhorn International Institute of Technology
// Thammasat University, Rangsit Campus
// http://www.siit.tu.ac.th
// 
// authors xx, bact', sea
// first created on: 2001 Jun xx
// last modified on: 2008 May 15 sea

include("settings/config.inc.php");
include("settings/accounts.inc.php");
include("settings/forum.inc.php");
include("lib/util.inc.php");
include("lib/users_function.inc.php");

global $db_link, $DGB;

extract($_POST);
extract($_GET);

function makeIcqStatus($uin) {
 $tmp = "";
 if($uin != "") {
  $tmp = "<a href=\"http://wwp.icq.com/".$uin."\" target=\"_blank\"><img alt=\"".$uin."\" border=0 width=18 height=18 src=\"http://online.mirabilis.com/scripts/online.dll?icq=".$uin."&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;img=5\"></a>";
 }
 return $tmp;
}

$Forum_ID = "-";
$Forum = "-";
$ForumIcon = "<img src=\"images/fi/default.gif\" width=\"16\" height=\"16\">";
$PosterName = "-";
$PosterEmail = "-";
$PosterICQ = "-";
$PostTime = "-";
$AnsNum = "-";
$ReadNum = "-";

$Title = "cannot connect to database";
$Message = "please contact administrator.";

$show_answer = true;

$query = "SELECT * FROM ".$DGB['DB_TOPICS_TABLE'].
  " WHERE Topic_ID=$Topic_ID AND Status<>0";
dgb_db_connect();
$result = mysql_query($query);

if ($result) {
 $row = mysql_fetch_array($result);
  
 $Title = "topic not found";
 $Message = "a topic with topic number specified was not found or has been deleted.";
  
 if ($row) {
  $Topic_ID = $row['Topic_ID'];
  $Forum_ID = $row['Forum_ID'];
  $Title = htmlspecialchars(stripslashes($row['Title']));
  $PosterName = registeruser(htmlspecialchars(stripslashes($row['PosterName'])));
  
  //Edited by Sea For fixing bug when viewing profile
  preg_match("~.*\((.*) \[RegisterUserLogin\]\)~",$row[PosterName],$RealPosterName);
  $RealPosterName = $RealPosterName[1];
  if ($RealPosterName==NULL){
  	  preg_match("~(.*)\[RegisterUserLogin\]~",$row[PosterName],$RealPosterName);
  	  $RealPosterName = $RealPosterName[1];
  }
  //$RealPosterName = str_replace("[RegisterUserLogin]","",$row['PosterName']);

  $PosterEmail = htmlspecialchars($row['PosterEmail']);
  $PosterICQ = makeIcqStatus(htmlspecialchars($row['PosterICQ']));
  //$IP = $row['IP'];
  $PostTime = $row['PostTime'];
  $AnsTime = $row['AnsTime'];
  $AnsNum = $row['AnsNum'];
  $ReadNum = $row['ReadNum']+1; //for display only, not related to db update

  $Message   = stripslashes($row['Message']);
  $Message   = nl2br($Message);
  $Message   = bbcodeparse($Message);
  $Message   = smileycodeparse($Message);

  $NowTimeStamp  = time();
  $NowTimeStamp_Offsetted  = $NowTimeStamp-($DGB["TIME_OFFSET"]*3600);
  $PostTimeStamp = mysql_datetime_to_timestamp($PostTime);
  $AnsTimeStamp  = mysql_datetime_to_timestamp($AnsTime);

  $PostTime = date($DGB["MEDIUM_DATETIME_FORMAT"], $PostTimeStamp);

  $Forum = $DGB_FORUM_NAME[$Forum_ID];

  $ForumIcon = forum_icon($Forum_ID, $AnsNum, $ReadNum, $PostTimeStamp, $AnsTimeStamp, $NowTimeStamp_Offsetted);
  $ForumIcon = "<img src=\"$ForumIcon\" width=\"16\" height=\"16\">";
    
  // update read number
  $query =
   "UPDATE ".$DGB['DB_TOPICS_TABLE'].
   " SET ReadNum=ReadNum+1 WHERE Topic_ID=$Topic_ID";

  $result = mysql_query($query);
  // end update read number
 } else {
  $show_answer = false;
  mysql_close();
 }
} else {
  $show_answer = false;
  mysql_close();
}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?=$DGB["ENCODING"];?>">
<meta http-equiv="Pragma" content="no-cache">
<title><?=$Title;?></title>
<link rel="stylesheet" href="<?=$DGB["STYLESHEET"];?>" type="text/css">

<script language="JavaScript">
<!--have

var remote=null;
function nw(n,u,w,h,x) {
 args="width="+w+",height="+h+",resizable=yes,scrollbars=yes,status=0";
 n = n+"<?=$Topic_ID;?>";
 remote=window.open(u,n,args);
 if (remote != null) {
  if (remote.opener == null)have
   remote.opener = self;
 }
 if (x == 1) { return remote; }
}

function put(m) {
 s = document.compose.msgdetail.value;
 document.compose.msgdetail.value = s + m;
}

//-->
</script>
</head>

<body bgcolor="#FFFFEE">
<table class="pHead" width="100%" border="0" cellspacing="0" cellpadding="0">
 <tr valign="middle">
  <td>
   <b><font color="#FFFFF"><?=$DGB["WEBBOARD_NAME"];?> / </font>read message</b>
  </td>
 </tr>
</table>

<table class="pSubHead" width="100%" border="0" cellspacing="0" cellpadding="0">
 <tr>
  <td valign="middle" class="pagesubheader" align="right"><?=$DGB["WEBBOARD_DESCRIPTION"];?> </td>
 </tr>
</table>
<br>
<!--END HEADER-->

<!--START QUESTION-->
<table width="80%" border="0" cellpadding="1" cellspacing="0" align="center" bgcolor="#cccccc">
 <tr>
  <td class="tpInfo">
   <?=$ForumIcon;?> topic
   id <b><?=$Topic_ID;?></b> |
   forum <b><?=$Forum;?></b> |
   read <b><?=$ReadNum;?></b> |
   answer <b><?=$AnsNum;?></b>
  </td>
 </tr>
 <tr>
  <td>
   <table width="100%" border="0" cellpadding="0" cellspacing="0" align="center" bgcolor="#fefefe">
    <tr class="tpTitle">
     <td colspan="2"> <?=$Title;?></td>
    </tr>
    <tr class="tpMsg">
     <td width="5%"> </td>
     <td><?=$Message;?></td>
    </tr>
    <tr class="tpPosterInfo" align="right">
     <td colspan="2">
        <?=$PostTime;?> - <?=$PosterICQ;?>&nbsp;&nbsp;<? if ($RealPosterName) {?><a class="tpPosterInfo"
      href="javascript:nw('profile','profile.php?ProfileName=<?=$RealPosterName;?>',350,500,0)"
      title="<?=$PosterEmail;?>"><b><?=$PosterName;?></b></a><?} else { echo "<h class='tpPosterInfo'>$PosterName</h>"; }?>
     </td>
    </tr>
   </table>
  </td>
 </tr>
</table>
<br>
<!--END QUESTION-->

<?php
if ($show_answer) {
 $query = "SELECT * FROM ".$DGB['DB_ANSWERS_TABLE'].
 " WHERE Topic_ID=$Topic_ID AND Display<>0 ORDER BY PostTime";
 $result = mysql_query($query);
  
 if ($result) {
  $row = mysql_fetch_array($result);
  $ans_no = 0;
  if ($row) {
   do{
    $ans_no++;
    $PosterName  = registeruser(htmlspecialchars(stripslashes($row['PosterName'])));

    //Edited by Sea For fixing bug when viewing profile
    preg_match("~.*\((.*) \[RegisterUserLogin\]\)~",$row[PosterName],$RealPosterName);
    $RealPosterName = $RealPosterName[1];
    if ($RealPosterName==NULL){
  	    preg_match("~(.*)\[RegisterUserLogin\]~",$row[PosterName],$RealPosterName);
  	    $RealPosterName = $RealPosterName[1];
    }
    //$RealPosterName = str_replace("[RegisterUserLogin]","",$row['PosterName']);

    $PosterEmail = htmlspecialchars(stripslashes($row['PosterEmail']));
    $PosterICQ   = makeIcqStatus(htmlspecialchars(stripslashes($row['PosterICQ'])));
    $PostTime    = $row['PostTime'];
    //$IP          = $row['IP'];

    $Message   = stripslashes($row['Message']);
    $Message   = nl2br($Message);
    $Message   = bbcodeparse($Message);
    $Message   = smileycodeparse($Message);
    //performance penalty str_replace
    //$Message   = blobcodeparse($Message);
    $PostTime = mysql_datetime_to_timestamp($PostTime);
    $PostTime = date($DGB["MEDIUM_DATETIME_FORMAT"], $PostTime);
  ?>
<!--START ANSWERS-->
<table width="80%" border=0 cellpadding=1 cellspacing=0 align="center" bgcolor="#cccccc">
 <tr>
  <td>
   <table width="100%" border=0 cellpadding=0 cellspacing=0 align="center" bgcolor="#fefefe">
    <tr class="tpMsg">
     <td width="5%" valign="top"><font size=1 color="#cccccc"> [<?=$ans_no;?>]</font></td>
     <td><?=$Message;?></td>
    </tr>
    <tr class="tpPosterInfo" align="right">
     <td colspan=2>
      <?=$PostTime;?> - <?=$PosterICQ;?>&nbsp;&nbsp;<? if ($RealPosterName) {?><a class="tpPosterInfo"
      href="javascript:nw('profile','profile.php?ProfileName=<?=$RealPosterName;?>',350,500,0)"
      title="<?=$PosterEmail;?>"><b><?=$PosterName;?></b></a><?} else { echo "<h class='tpPosterInfo'>$PosterName</h>"; }?>
     </td>
    </tr>
   </table>
  </td>
 </tr>
</table>
<br>
<? } while($row = mysql_fetch_array($result));
  }
 }
 mysql_close();
?>
<!--END ANSWERS-->

<?php
// check user
if ($DGB["ENABLE_LOGIN"] == true) {
 global $DGB, $db_link;
 $UserDetail = SelectUserDetail($_COOKIE[Username], $_COOKIE[Password], $db_link, $DGB['DB_USERS_TABLE']);
 list($Email,$ICQ,$MSN)= explode("|",$UserDetail);
}
?>

<!--START REPLY FORM-->
<form name="compose" method="post" action="reply.php">
<input type="hidden" name="Topic_ID" value="<?=$Topic_ID;?>">
<table width="80%" border=0 align="center" cellpadding=1 cellspacing=0 bgcolor="#cccccc">
<tr bgcolor="#999999">
 <td><img src="images/i_balloon.gif" width=15 height=13> <font color="#ffffff"><b>post reply</b></font></td>
</tr>
<tr>
<td>
<table width="100%" border=0 align="center" bgcolor="#fefefe">
<tr>
<td valign="bottom">

<table class="rf" border=0 align="center">
 <tr valign="top">
  <td nowrap>&nbsp;
  </td>
  <td><a href="javascript:put('[b][/b]')"><img src="images/bold.gif" alt="Bold" width="16" height="16" border="0"></a> <a href="javascript:put('[i][/i]')"><img src="images/italics.gif" alt="Italics" width="16" height="16" border="0"></a> <a href="javascript:put('[u][/u]')"><img src="images/underline.gif" alt="Underline" width="16" height="16" border="0"> </a><a href="javascript:put('[sup][/sup]')"><img src="images/sup_script.gif" alt="Superscript" width="16" height="16" border="0"></a> <a href="javascript:put('[sub][/sub]')"><img src="images/sub_script.gif" alt="Subscript" width="16" height="16" border="0"></a> <a href="javascript:put('[quote][/quote]')"><img src="images/quote.gif" width="16" height="16" border="0"></a> <a href="javascript:nw('smilies','smilies/smilies.html',330,300,0)"><img src="images/smiley.gif" alt="Smiley" width="16" height="16" border="0"></a> <a href="javascript:put('[www][/www]')"><img src="images/links.gif" alt="Links" width="16" height="16" border="0"> </a>
    <?php if ($DGB["ENABLE_UPLOAD"] == true) 
   { echo "<a href=\"javascript:nw('upload','upload_ui.php?Topic_ID=0000',350,250,0)\" title=\"upload pictures, photos\"><img src=\"images/i_upload.gif\" width=19 height=19 border=0 align=\"middle\"></a>" ;
   }
   ?>
    <a href="javascript:put('[mail][/mail]')"><img src="images/email.gif" alt="e-mail" width="16" height="16" border="0"></a>
  </td>
 </tr>
 <tr valign="top">
   <td nowrap>text:
     <hr width="98%">
     <font color="#666666"><font size="-2">toolbox</font><br>
     <a class="textTool" href="javascript:nw('smilies','smilies/smilies.html',330,300,0)" title="smilies, emoticons and extra icons"><img src="images/i_smilies.gif" width=19 height=19 border=0 align="middle"> smilies</a><br>
     <a class="textTool" href="javascript:nw('deco','html/textdeco.html',400,300,0)" title="text decoration, making links to web, e-mail and picture"><img src="images/i_textdeco.gif" width=19 height=19 border=0 align="middle"> deco</a>
     <?php
   if ($DGB["ENABLE_UPLOAD"] == true) {
    echo "<br><a class=\"textTool\" href=\"javascript:nw('upload','upload_ui.php?Topic_ID=$Topic_ID',350,250,0)\" title=\"upload pictures, photos\"><img src=\"images/i_upload.gif\" width=19 height=19 border=0 align=\"middle\"> upload</a>";
   }
   ?>
     </font></td>
   <td><textarea class="rfc" name="msgdetail" cols=40 rows=6 wrap="VIRTUAL"></textarea></td>
 </tr>

<?php
$qLastestTopic = mysql_query("SHOW TABLE STATUS LIKE 'wb_topics'") or die ( "Query failed: " . mysql_error() . "<br/>");
$rLastestTopic = mysql_fetch_assoc($qLastestTopic);
$LastestTopic = $rLastestTopic['Auto_increment'];
if ($LastestTopic - $Topic_ID > 500){
	echo 	'<tr>
		  <td>verify :</td>
		  <td colspan=2>
		   <img src="captcha/imagebuilder.php" border="1"><br>
		   Please enter the code shown above.<br>
		   <input class="rfc" MAXLENGTH=8 SIZE=8 name="userstring" type="text" value=""><br> 
		  </td>
		 </tr>';
}
?>
 <tr valign="top">
  <td>from:</td>
  <td><input class="rfc" size="25" type="text" name="msgby" 
   <? if($DGB["ENABLE_LOGIN"]) { if(!empty($_COOKIE[Username])){echo "value = '$_COOKIE[Username]'";} }?> ></td>
 </tr>
 <tr class="rf_opt" valign="top">
  <td>email:</td>
  <td><input class="rfc_opt" size="25" type="text" name="email"
   <? if($DGB["ENABLE_LOGIN"]) { if(!empty($Email)){echo "value = '$Email'";}}?>> <font size="1">(optional)</td>
 </tr>
 <tr class="rf_opt" valign="top">
  <td>ICQ:</td>
  <td><input class="rfc_opt" size="25" type="text" name="icq"
   <? if($DGB["ENABLE_LOGIN"]) { if(!empty($ICQ)){echo "value = '$ICQ'";}}?>> <font size="1">(optional)</td>
 </tr>
 <tr>
  <td> </td>
  <td>
   <input class="rfc_btn" type="submit" value=" post message " name="submit"> 
   <input class="rfc_btn" type="reset" value=" clear all " name="reset">
  </td>
 </tr>
 <tr class="rf_opt">
  <td colspan="2" align="center">hit 'post message' only once</td>
 </tr>
</table>

</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
<!--END REPLY FORM-->
<?  } ?>

<?php
 include("html/footer.html");
?>
</body>
</html>

